/**
 * Created by MLS on 15/11/13.
 */
(function($){
    var pageTabel = $('#pageMessage').DataTable({
        "ajax": "user/searchUser",
        "paging": true,
        "searching": false,
        "lengthChange": false,
        "info": true,
        "pageLength":20,
        //"lengthChange":true,
        "autoWidth": false,
        "order": [],
        "columns":[
            {"data":"_id"},
            {"data":"loginName"},
            {"data":"nickName"},
            {"data":"phone"},
            {"data":"createUser"},
            {"data":"createTime"}
        ],
        "columnDefs": [
            {
                "targets": [0],
                "data": "description",
                "render": function ( data, type, full, meta ) {
                   var html =  '<div class="btn-group" role="group" aria-label="...">'+
                       '<button type="button" class="btn btn-info" edit-user="'+data+'">编辑</button>'+
                       ' <button type="button" class="btn btn-info" reset-Pwd="'+data+'">重置密码</button>'+
                       ' <button type="button" class="btn btn-danger" del-user="'+data+'">删除</button>'+
                       '</div>';
                    return html;
                }
            }
        ],
        "createdRow": function( row, data, dataIndex ) {
        $(row).find("[edit-user]").on("click",function(){initMessage(data);});
        $(row).find("[reset-Pwd]").on("click",function(){resetPwd(data);});
        $(row).find("[del-user]").on("click",function(){delUser(data);});
    }
    });

    $("#addUserBut").on("click",function(){
        initMessage();
    });


    function delUser(user){
        restAjax("user/delUser",window.RequestMethod.GET,{
            data:{"_id":user._id},
            success:function(data){
                if(!data.suc){
                    window.Dialog.alert(data.message);
                }else{
                    pageTabel.ajax.reload();
                }
            }
        });
    }

    function resetPwd(user){
        window.Dialog.prompt({
            title:"请输入新密码",
            func:function(remark){
                restAjax("user/resetPwd",window.RequestMethod.POST,{
                    data:{"_id":user._id,"password":remark},
                    success:function(data){
                        if(!data.suc){
                            window.Dialog.alert(data.message);
                        }else{
                            pageTabel.ajax.reload();
                        }
                    }
                });
            }
        })
    }

    function initMessage(user){
        if(user){
            $("#loginName").val(user.loginName);
            $("#nickName").val(user.nickName);
            $("#phone").val(user.phone);
            $("#_id").val(user._id);
        }else{
            $("#userForm").find("input").val("");
        }
        $('#userMessage').modal({backdrop:false});
    }

    $("#userForm").validation({icon:true});
    $("#userSave").on("click",function(){
        if($("#userForm").valid()){
            restAjax("user/saveUser",window.RequestMethod.POST,{formId:"userForm",success:function(data){
                if(!data.suc){
                    window.Dialog.alert(data.message);
                }else{
                    $('#userMessage').modal('hide')
                    pageTabel.ajax.reload();
                }
            }});
        }    });


    $("[print]").on("click",function(obj){
        var print = $(obj.currentTarget).attr("print");
        var data = $("#"+print).val();
        var json = $.parseJSON(data);
        var templateId = $("#printTemp").val();
        var printNum = $("#printNum").val();
        window.WmsPrint.printSkuBarcode(json,templateId,printNum)    });

})(jQuery);